package com.tiantian.quanminxiu.presenter;

import android.util.Log;

import com.tiantian.quanminxiu.api.AnchorService;
import com.tiantian.quanminxiu.utils.ApplicationBase;
import com.tiantian.quanminxiu.utils.Constants;
import com.tiantian.quanminxiu.utils.UserData;
import com.tiantian.quanminxiu.view.DialogGiftListView;

import rx.android.schedulers.AndroidSchedulers;

/**
 * Created by admin on 2016/8/11.
 */
public class DialogGiftListPresent implements Presenter<DialogGiftListView> {

    private DialogGiftListView dialogGiftListView;

    @Override
    public void attachView(DialogGiftListView view) {
        this.dialogGiftListView = view;
    }

    @Override
    public void detachView() {
        dialogGiftListView = null;
    }


    public void initGiftList(){
        if(UserData.Current.GiftList!=null&&UserData.Current.GiftList.length()>0){
            if(dialogGiftListView!=null){
                dialogGiftListView.getGiftList(UserData.Current.GiftList, UserData.Base.UserPackage);
                return;
            }
        }
        ApplicationBase app = ApplicationBase.get(dialogGiftListView.getContext());
        AnchorService service = app.getAnchorService();
        service.giftlist(UserData.Current.mid,UserData.Current.sign,Constants.APP_TYPE)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(app.defaultSubscribeScheduler())
                .subscribe(str -> {
                    Log.i("jj","--str:"+str.toString());
                    if(dialogGiftListView!=null){
                        dialogGiftListView.getGiftList(str.optJSONArray("data"), UserData.Base.UserPackage);
                    }
                });
        }

    }
